package com.service;

import com.dao.TAuthorDao;
import com.entity.TAuthor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * Created by del on 16-5-1.
 */
@Service
public class AuthorService {

    private static Logger LOG = LoggerFactory.getLogger(AuthorService.class);

    @Autowired
    private TAuthorDao tAuthorDao;


    public Boolean addOrUpdate(TAuthor tAuthor){
        TAuthor result = tAuthorDao.selectByUid(tAuthor.getUid());
        if (result == null){
            boolean isSucceed = tAuthorDao.insert(tAuthor);
            if (isSucceed){
                LOG.info("新增用户成功:{}",tAuthor);
            }
            else {
                LOG.info("新增用户失败:{}",tAuthor);
            }
            return isSucceed;
        }
        else {
//            tAuthor.setId(result.getId());
//            boolean isSucceed = null != tAuthorDao.update(tAuthor);
//            if (isSucceed){
//                LOG.info("更新用户成功:{}",tAuthor);
//            }
//            else {
//                LOG.info("更新用户失败:{}",tAuthor);
//            }
//            return isSucceed;
            return true;
        }
    }

    public Boolean remove(Integer id){
        return tAuthorDao.delete(id);
    }

    public TAuthor modify(TAuthor tAuthor){
        return tAuthorDao.update(tAuthor);
    }

    public TAuthor get(Integer id){
        return tAuthorDao.select(id);
    }

    public TAuthor getNewest(){
        return tAuthorDao.selectNewest();
    }
}
